import axios from 'axios';
import Cookies from 'js-cookie';
import store from '../store/index';
import router from '../router/index';
let util = {};
util.showThisRoute = function (itAccess, currentAccess) {
    if (typeof itAccess === 'object' && Array.isArray(itAccess)) {
        return util.oneOf(currentAccess, itAccess);
    } else {
        return itAccess === currentAccess;
    }
};

let ajaxUrl = 'http://api.gis.crystoneiot.com';

//let ajaxUrl = 'http://192.168.1.57:9992'
util.ajax = axios.create({
    baseURL: ajaxUrl,
    timeout: 10000
});

// Request拦截器
util.ajax.interceptors.request.use(function (config) {
    //console.log(sessionStorage.getItem('api_token'))
    if (sessionStorage.getItem('api_token')) {
        config.headers.Authorization = ' Token '+sessionStorage.getItem('api_token');
    }
    return config;
}, function (error) {
    return Promise.reject(error);
});
// Response拦截器
util.ajax.interceptors.response.use(function (response) {
    return response;
}, function (error) {
    if (error.response) {
        switch (error.response.status) {
            case 401:
                router.replace({name: 'login'});
                return false;
        }
    }
    return Promise.reject(error);
});

export default util;